import React from 'react'
import { Switch,Route, Redirect } from 'react-router-dom'
import Admin from 'pages/Admin'
import Login from 'pages/Login'
import NotFound from 'pages/NotFound'
const App = ()=>{
  return (
    <div>
      <Switch>
        <Route 
          path="/admin"
          render={
            (routerProps)=>{
              // 判断是否登录 没有登录则 跳转到登录 否则放行
              const userInfo = localStorage.getItem('userInfo')
              ?
              JSON.parse(localStorage.getItem('userInfo'))
              :
              {}
              const token = userInfo.token
              if(token){
                // 登录了
                return <Admin {...routerProps}/>
              }else{
                // 没有登录 重定向到登录
                return <Redirect to="/login"/>
              }
            }
          }
        />
        <Route 
          path="/login"
          component={Login}
        />
        <Route 
          path="/404"
          component={NotFound}
        />
        <Redirect to="/admin" from="/" exact/>
        <Redirect to="/404"/>
      </Switch>
    </div>
  )
}

export default App